package it.valeandre.omino.adapter;

import it.valeandre.omino.InterFragmentInterface;
import it.valeandre.omino.R;
import it.valeandre.omino.model.Ranking;
import it.valeandre.omino.model.RankingItem;
import it.valeandre.omino.utils.Maps;
import it.valeandre.omino.utils.ResBundle;
import it.valeandre.omino.utils.Utils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

public class RankingAdapter extends BaseAdapter implements ListAdapter {

	private LayoutInflater mInflater;
	private Ranking mRanking = new Ranking();
	private Context mContext;
	private InterFragmentInterface mInterFragmentInterface;
	
	public RankingAdapter(Context context) {
		mInterFragmentInterface = (InterFragmentInterface) context;
		mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setData(Ranking ranking) {
		mRanking = ranking;
		notifyDataSetChanged();
	}

	public int getCount() {
		return mRanking.rows.size();
	}

	public RankingItem getItem(int pos) {
		return mRanking.rows.get(pos);
	}

	public long getItemId(int pos) {
		return pos;
	}

	public View getView(int position, View convertView, ViewGroup adapterView) {
		View view = convertView;
		// if (view == null) {
		view = mInflater.inflate(R.layout.ranking_item, null);
		// }
		RankingItem rankingItem = getItem(position);
        LinearLayout backgroundHolder = (LinearLayout) view.findViewById(R.id.ranking_item_background_holder);
		ImageView logo = (ImageView) view.findViewById(R.id.ranking_team_logo);
		TextView rankingPosition = (TextView) view.findViewById(R.id.ranking_team_position);
		TextView team = (TextView) view.findViewById(R.id.ranking_team_label);
		TextView points = (TextView) view.findViewById(R.id.ranking_item_points);
		TextView played = (TextView) view.findViewById(R.id.statistic_played);
		TextView won = (TextView) view.findViewById(R.id.statistic_won);
		TextView draw = (TextView) view.findViewById(R.id.statistic_draw);
		TextView lost = (TextView) view.findViewById(R.id.statistic_lost);
		ImageView arrow = (ImageView) view.findViewById(R.id.ranking_team_arrow);
		
		if (position <= 1) {
            backgroundHolder.setBackgroundResource(R.drawable.violet_list_item_background_normal);
			/*rankingPosition.setTextColor(mContext.getResources().getColor(R.color.red_champions));
			team.setTextColor(mContext.getResources().getColor(R.color.red_champions));
			points.setTextColor(mContext.getResources().getColor(R.color.red_champions));*/
		} else if (position == 2) {
            backgroundHolder.setBackgroundResource(R.drawable.blue_list_item_background_normal);
			/*rankingPosition.setTextColor(mContext.getResources().getColor(R.color.orange_qual_champions));
			team.setTextColor(mContext.getResources().getColor(R.color.orange_qual_champions));
			points.setTextColor(mContext.getResources().getColor(R.color.orange_qual_champions));*/
		} else if (position > 2 && position <= 4) {
            backgroundHolder.setBackgroundResource(R.drawable.green_list_item_background_normal);
			/*rankingPosition.setTextColor(mContext.getResources().getColor(R.color.blue_light));
			team.setTextColor(mContext.getResources().getColor(R.color.blue_light));
			points.setTextColor(mContext.getResources().getColor(R.color.blue_light));*/
		} else if (position >= 17) {
            backgroundHolder.setBackgroundResource(R.drawable.red_list_item_background_normal);
			/*rankingPosition.setTextColor(mContext.getResources().getColor(R.color.pink_europa_league));
			team.setTextColor(mContext.getResources().getColor(R.color.pink_europa_league));
			points.setTextColor(mContext.getResources().getColor(R.color.pink_europa_league));*/
		} else {
            backgroundHolder.setBackgroundResource(R.drawable.dark_list_item_background_normal);
			/*rankingPosition.setTextColor(mContext.getResources().getColor(R.color.blue_header));
			team.setTextColor(mContext.getResources().getColor(R.color.blue_header));
			points.setTextColor(mContext.getResources().getColor(R.color.blue_header));*/
		}

		rankingPosition.setText((position + 1) + ".");
		team.setText(rankingItem.cols.get(0).value.replace("(*)", ""));
		final String teamvalue = team.getText().toString();

        Utils.setLogoByName(teamvalue.replace("ChievoVerona", "Chievo"), logo, Maps.LOGOS_DIMENSION_MID_ID);
//		for(String keyTeam: Maps.TEAM_LOGO_MAP.keySet()) {
//			if(teamvalue.contains(keyTeam)) {
//				logo.setImageResource(Maps.TEAM_LOGO_MAP.get(keyTeam));
//			}
//		}
		
		points.setText(rankingItem.cols.get(2).value);
		played.setText(rankingItem.cols.get(3).value);
		won.setText(rankingItem.cols.get(4).value);
		draw.setText(rankingItem.cols.get(5).value);
		lost.setText(rankingItem.cols.get(6).value);
		
		Button loadFeed = (Button) view.findViewById(R.id.team_feeds_button);
		
		if(ResBundle.TEAM_NEWS_ENABLED) {
			loadFeed.setVisibility(View.VISIBLE);
			loadFeed.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Utils.log("team value = " + teamvalue);
					String feedUrl =  Maps.NEWS_FEED_MAP.get(teamvalue.trim());
					Utils.log("feed URL = " + feedUrl);
					if (feedUrl != null && feedUrl.length() > 0) {
						mInterFragmentInterface.switchToFeedsFragment(feedUrl);
					} else {
						mInterFragmentInterface.showDialog(mContext.getResources().getString(R.string.no_feed_available));
					}
				}
			});
		} else {
			loadFeed.setVisibility(View.GONE);
		}
		
		
		return view;
	}
	
	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public int getViewTypeCount() {
		return 1;
	}

}
